#include "favoritestab.h"
#include <QPainter>
#include <QFontMetrics>

using namespace ExecTerminalImplNS;

const QColor LINE_COLOR(119, 119, 119);
const QColor UNCHECKED_LINE_COLOR(170, 170, 170);
const QColor UNCHECKED_TEXT_COLOR(170, 170, 170);
const int TEXT_HOR_MARGIN = 8;
const int TEXT_TOP_MARGIN = 14;
const int TAB_HEIGHT = 20;

FavoritesTab::FavoritesTab(const QString & aText, QWidget* parent): AutoExclusiveButton(aText, parent)
{
    resizeSelf();
    setUncheckedTextColor(UNCHECKED_TEXT_COLOR);
    setTextXPos(TEXT_HOR_MARGIN);
    setTextYPos(TEXT_TOP_MARGIN);
}

void FavoritesTab::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    const bool chckd = isChecked();
    const int rightX = width() - 1;
    const int bottomY = chckd ? height() - 1: height() - 2;
    const int leftX = 0;
    const int topY = 0;
    if (chckd)
    {
        painter.setPen(LINE_COLOR);
        painter.fillRect(leftX + 1, topY + 1, width() - 2, height() - 1, palette().base());
    }
    else
    {
        painter.setPen(UNCHECKED_LINE_COLOR);
    }
    painter.drawLine(leftX, bottomY, leftX, topY);
    painter.drawLine(leftX, topY, rightX, topY);
    painter.drawLine(rightX, topY, rightX, bottomY);
    drawButtonText(&painter);        
}

void FavoritesTab::textChanged()
{
    resizeSelf();
}

void FavoritesTab::resizeSelf()
{
    QFontMetrics metrics(font());
    const int textWidth = metrics.width(text());
    resize(textWidth + 2 * TEXT_HOR_MARGIN, TAB_HEIGHT);
}


